import SwiftUI
import GoogleMobileAds

struct SplashView: View {
    @State private var progress: Double = 0
    @State private var progressInt: Int = 0
    @State private var isCompleted: Bool = false
    @State private var adLoaded: Bool = false
    @State private var closeAd: Bool = false
    @StateObject var adManager = AppOpenAdManager.shared

    var body: some View {
        NavigationView {
            VStack {
                Spacer()

                Image("icon")
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: 200.0, height: 200.0)
                    .cornerRadius(10.0)

                ProgressView(value: progress, total: 100)
                    .padding([.leading, .trailing], 5)
                    .frame(width: 200)
                    .progressViewStyle(LinearProgressViewStyle(tint: Color("coco")))
                    .padding(.top, 30)
                   
                Spacer()

                Image("st")
                    .resizable(resizingMode: .stretch)
                    .aspectRatio(contentMode: .fill)
                    .frame(maxWidth: .infinity, maxHeight: 200)
                    .edgesIgnoringSafeArea(.all)
            }
            .onAppear {
                
             let openStatus =    UserDefaultsManager.shared.getBool(forKey: "openStatus")
                if openStatus{
                    startGoogleMobileAdsSDK()

                }
                Timer.scheduledTimer(withTimeInterval: 0.03, repeats: true) { timer in
                    progress += 1
                    progressInt += 1
                    if progressInt >= 100 {
                        timer.invalidate()
                        adLoaded = true
                        checkCompletion()
                    }
                }
            }

            .onReceive(adManager.$adClosed) { adClosed in
                if adClosed {
                    closeAd = true
                    checkCompletion()  // 检查跳转条件
                }
            }
            .navigationBarHidden(true)
            .background(
                NavigationLink(destination: ContentView(), isActive: $isCompleted) {
                    EmptyView()
                }
                .hidden()
            )
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }

    private func checkCompletion() {
        let openStatus =  UserDefaultsManager.shared.getBool(forKey: "openStatus")
        if !openStatus{
            isCompleted = true
        }
        if adLoaded && closeAd {
            isCompleted = true
        }
    }

    private func startGoogleMobileAdsSDK() {
        DispatchQueue.main.async {
            GADMobileAds.sharedInstance().start { status in
                Task {
                    await AppOpenAdManager.shared.loadAd()
                    DispatchQueue.main.async {
                        AppOpenAdManager.shared.showAdIfAvailable()
                    }
                }
            }
        }
    }

    func menuButton(imageName: String, title: String) -> some View {
        VStack(spacing: 0) {
            HStack {
                Image(imageName)
                    .resizable()
                    .scaledToFit()
                    .frame(width: 24, height: 24)
                Text(title)
                    .font(.system(size: 16))
                    .padding(.leading, 8)
                Spacer()
            }
            .padding(.all, 10)

            Rectangle()
                .foregroundColor(.blue)
                .frame(maxWidth: .infinity, maxHeight: 10)
        }
    }
}

#Preview {
    SplashView()
}

struct MenuItem: View {
    var imageName: String
    var text: String
    var rectangleHeight: CGFloat

    var body: some View {
        VStack(spacing: 0) {
            HStack {
                Spacer()
                Image(imageName)
                Text(text)
                    .frame(width: 100, alignment: .leading) // 设置固定宽度
                Spacer()
            }
            .padding(.all, 10)

            if rectangleHeight > 0 {
                Rectangle()
                    .foregroundColor(Color("mainback"))
                    .frame(maxWidth: .infinity, maxHeight: rectangleHeight)
            }
        }
    }
}
